package aiyh.utils.httpUtil.httpAsync;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import aiyh.utils.httpUtil.ExtendedIOUtils;

import java.util.function.Consumer;

/**
 * @author EBU7-dev1-ayh
 * @date 2021/9/2 0002 23:18
 * callback
 */


public class HttpAsyncThreadCallBack implements Runnable{
	private final CloseableHttpClient httpClient;
	private final HttpUriRequest request;
	private final Consumer<CloseableHttpResponse> consumer;

	public HttpAsyncThreadCallBack(CloseableHttpClient httpClient, HttpUriRequest request, Consumer<CloseableHttpResponse> consumer){
		this.httpClient = httpClient;
		this.request = request;
		this.consumer = consumer;
	}

	@Override
	public void run() {
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(request);
			consumer.accept(response);
		} catch (Exception e) {
			consumer.accept(null);
		}
		ExtendedIOUtils.closeQuietly(httpClient);
		ExtendedIOUtils.closeQuietly(response);
	}
}
